﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class GuarCompanyBiz
    {
        /// <summary>
        /// 获取所有担保公司信息
        /// </summary>
        /// <returns></returns>
        public static List<GuaranteeCompany> GetGuaranteeCompanyList()
        {
            try
            {
                var guarCompanyList = new GuaranteeCompany().FindAll();
                return guarCompanyList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 获取所有担保公司信息
        /// </summary>
        /// <returns></returns>
        public static List<GuaranteeCompany> GetGuaranteeCompanyList(string key)
        {
            try
            {
                var guarCompanyList = new GuaranteeCompany().FindAll(c=>c.CompanyName.Contains(key));
                return guarCompanyList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 根据状态获取担保公司
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<GuaranteeCompany> GetGuaranteeCompanyByStatus(int status)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var guarCompanyList = (from c in ctx.GuaranteeCompany where c.Status == status orderby c.ID select c);
                    var data = guarCompanyList.ToList().Select(p => new GuaranteeCompany().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 分页获取担保公司信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<GuaranteeCompany> GetGuarCompanyListByPage(int page, int pageSize, string key, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                    var guarCompanyList = (from c in ctx.GuaranteeCompany where c.Status != (int)ItemStatus.Delete
                                           && (c.CompanyName.Contains(key) || c.Summary.Contains(key) || c.Description.Contains(key))
                                           orderby c.CreateTime descending select c);
                    count = guarCompanyList.Count();
                    var data = guarCompanyList
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new GuaranteeCompany().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 增加担保公司
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="logoImage"></param>
        /// <param name="summary"></param>
        /// <param name="description"></param>
        /// <param name="qualificationsImages"></param>
        /// <param name="agreementImages"></param>
        /// <param name="status"></param>
        /// <param name="createUser"></param>
        /// <param name="createTime"></param>
        /// <returns></returns>
        public static bool GuarCompanyAdd(string companyName, string logoImage, string summary, string description, string qualificationsImages, string agreementImages)
        {
            try
            {
                var guarCompany = new GuaranteeCompany()
                {
                    CreateUser = System.Web.HttpContext.Current.User.Identity.Name,
                    AgreementImages = agreementImages,
                    CreateTime = DateTime.Now,
                    CompanyName = companyName,
                    Description = description,
                    LogoImage = logoImage,
                    QualificationsImages = qualificationsImages,
                    Summary = summary,
                    Status = ItemStatus.Enable
                };
                return guarCompany.PostAdd() != null;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return false;
        }

        /// <summary>
        /// 根据ID获取担保公司信息
        /// </summary>
        /// <param name="policyId"></param>
        /// <returns></returns>
        public static GuaranteeCompany GetGuarCompanyByID(int guarCompanyId)
        {
            try
            {
                return new GuaranteeCompany().Find(guarCompanyId.ToString());
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 修改担保公司信息
        /// </summary>
        /// <param name="guarCompanyId"></param>
        /// <param name="companyName"></param>
        /// <param name="logoImage"></param>
        /// <param name="summary"></param>
        /// <param name="description"></param>
        /// <param name="qualificationsImages"></param>
        /// <param name="agreementImages"></param>
        /// <param name="status"></param>
        /// <param name="createUser"></param>
        /// <param name="createTime"></param>
        /// <returns></returns>
        public static bool GuarCompanyEdit(int guarCompanyId, string companyName, string logoImage, string summary, string description, string qualificationsImages, string agreementImages)
        {
            try
            {
                var guarCompany = new GuaranteeCompany().Find(guarCompanyId.ToString());
                if (guarCompany != null)
                {
                    guarCompany.CreateUser = System.Web.HttpContext.Current.User.Identity.Name;
                    guarCompany.AgreementImages = agreementImages;
                    guarCompany.CreateTime = DateTime.Now;
                    guarCompany.CompanyName = companyName;
                    guarCompany.Description = description;
                    guarCompany.LogoImage = logoImage;
                    guarCompany.QualificationsImages = qualificationsImages;
                    guarCompany.Summary = summary;
                    return guarCompany.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return false;
        }

        /// <summary>
        /// 修改担保公司状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool GuarCompanyStatusChange(int guarCompanyId, int destinatStatus)
        {
            try
            {
                var guarCompany = new GuaranteeCompany().Find(guarCompanyId.ToString());
                if (guarCompany != null)
                {
                    guarCompany.Status = (ItemStatus)destinatStatus;
                    return guarCompany.PostModify() != null;
                };
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                return false;
            }
            return false;
        }
    }
}
